class School:
    """学院类"""
    def __init__(self, name: str):
        self.name = name
        self.departments = []  # 学院包含多个系（组合关系）
        self.students = []     # 学院拥有多个学生（聚合关系）
    
    def add_department(self, department: 'Department') -> None:
        """添加系到学院"""
        if department not in self.departments:
            self.departments.append(department)
    
    def enroll_student(self, student: 'Student') -> None:
        """学生注册到学院"""
        if student not in self.students:
            self.students.append(student)


class Department:
    """系类"""
    def __init__(self, name: str):
        self.name = name
        self.instructors = []  # 系拥有指导老师（组合关系）
        self.subjects = []     # 系提供课程（组合关系）
        self.students = []     # 系拥有学生
    
    def add_instructor(self, instructor: 'Instructor') -> None:
        """添加指导老师到系"""
        if instructor not in self.instructors:
            self.instructors.append(instructor)
    
    def add_subject(self, subject: 'Subject') -> None:
        """添加课程到系"""
        if subject not in self.subjects:
            self.subjects.append(subject)
    
    def add_student(self, student: 'Student') -> None:
        """添加学生到系"""
        if student not in self.students:
            self.students.append(student)


class Instructor:
    """指导老师类"""
    def __init__(self, name: str):
        self.name = name
        self.subjects = []     # 老师教授课程（关联关系）
    
    def teach_subject(self, subject: 'Subject') -> None:
        """老师教授课程"""
        if subject not in self.subjects:
            self.subjects.append(subject)


class Subject:
    """课程类"""
    def __init__(self, code: str, name: str, credits: int):
        self.code = code
        self.name = name
        self.credits = credits
        self.students = []     # 课程被学生选修（关联关系）
    
    def add_student(self, student: 'Student') -> None:
        """学生选修课程"""
        if student not in self.students:
            self.students.append(student)


class Student:
    """学生类"""
    def __init__(self, student_id: str, name: str):
        self.student_id = student_id
        self.name = name
        self.subjects = []     # 学生参加课程（关联关系）
    
    def take_subject(self, subject: 'Subject') -> None:
        """学生参加课程学习"""
        if subject not in self.subjects:
            self.subjects.append(subject)


# 约束条件验证函数
class ConstraintValidator:
    """约束条件验证器"""
    
    @staticmethod
    def validate_school_departments(school: School) -> bool:
        """验证：一个学院由1到多个系构成"""
        return len(school.departments) >= 1
    
    @staticmethod
    def validate_department_subjects(department: Department) -> bool:
        """验证：每个系提供至少一门课程"""
        return len(department.subjects) >= 1
    
    @staticmethod
    def validate_department_instructors(department: Department) -> bool:
        """验证：每个系至少拥有1位指导老师"""
        return len(department.instructors) >= 1
    
    @staticmethod
    def validate_student_subjects(student: Student) -> bool:
        """验证：学生参加1-5门课程学习"""
        return 1 <= len(student.subjects) <= 5
    
    @staticmethod
    def validate_instructor_subjects(instructor: Instructor) -> bool:
        """验证：指导老师教授课程（1-3门）"""
        return 1 <= len(instructor.subjects) <= 3
